package com.palm.vue;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MJsUtil {
    private static Pattern importPattern = Pattern.compile("import\\s+([\\{\\w\\,\\}\\s]+\\s+from\\s+)?(['\"][\\.\\-\\/\\w]+['\"])",Pattern.DOTALL );

    public static String toAmd(String source){
        //转换导出
        source = rewriteExports(source);//source.replaceFirst("(^|\\s)export\\s+default\\s*", "\n module.exports = ");
        //转换导入
        Matcher importMatcher=importPattern.matcher(source);
        StringBuffer sb = new StringBuffer() ;
        while (importMatcher.find()){
            String g1=importMatcher.group(1);
            String g2=importMatcher.group(2);
            if(g1==null){
                importMatcher.appendReplacement(sb,"require(" + g2 + ")");
            }else{
                String name = g1.replaceAll("\\s+from\\s+$", "");
                name=name.replaceAll("\\s+as+\\s+",":");
                importMatcher.appendReplacement(sb,"const " + name + " = require(" + g2 + ")");
            }
        }
        importMatcher.appendTail(sb) ;

        return "define(function(require,exports,module){\n"+sb+"})";
    }
    private static final Set<String> varDefine = new HashSet<>(Arrays.asList("let", "var", "const"));
    private static final Pattern exportPattern = Pattern.compile("(^|\\s)export\\s+(((let|const|var)\\s+)|(default\\s+)|((class|function)\\s+\\w+)|(\\{.+?\\}\\s*(from\\s+(['\"][\\.\\-\\/\\w]+['\"]))?))", Pattern.DOTALL);

    private static String rewriteExports(String source){
       Matcher importMatcher = exportPattern.matcher(source);
        StringBuffer sb = new StringBuffer();
        while (importMatcher.find()) {
            String g1 = importMatcher.group(1);
            String g2 = importMatcher.group(2).trim();
            if (varDefine.contains(g2)) {
                importMatcher.appendReplacement(sb, g1 + "exports.");
            } else if ("default".equals(g2)) {
                importMatcher.appendReplacement(sb, g1 + "module.exports=");
            } else if (g2.startsWith("function")) {
                importMatcher.appendReplacement(sb, g1 + "exports." + g2.substring(9).trim() + "=function");
            } else if (g2.startsWith("class")) {
                importMatcher.appendReplacement(sb, g1 + "exports." + g2.substring(5).trim() + "=class");
            } else if (g2.startsWith("{")) {
                System.out.println(g2);
                if (g2.endsWith("}")) {
                    String to = g2.replaceAll("(\\w+)\\s+as\\s+(\\w+)", "$2:$1");
                    importMatcher.appendReplacement(sb, g1 + "Object.assign(exports," + to + ")");
                } else {
                    String to = g2.replaceAll("(\\w+)\\s+as\\s+(\\w+)", "$1:$2").replaceFirst("\\s+from\\s+('|\")", "=require($1");
                    String obj = to.substring(0, to.lastIndexOf('}')).replaceAll("\\w+:", "");
                    to = g1 + "const " + to + ");Object.assign(exports," + obj + ");";
                    importMatcher.appendReplacement(sb, to);
                }
            }
        }
        importMatcher.appendTail(sb);
        return sb.toString();
    }
}
